from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from django.core.paginator import Paginator
from django.db.models import Max,Q,F,Min,Avg,Count,Sum
from django.contrib.auth.hashers import make_password,check_password
import datetime  #导入时间包
import time
from .ser import *
# Create your views here.

# #手机短信
from ronglian_sms_sdk import SmsSDK
import random
accId = '8aaf07087bc82708017bf7aae5000e27'
accToken = 'f53d9d4740ed40adb6829538dc940709'
appId = '8aaf07087bc82708017bf7aae5e00e2e'
import json
def send_message(mobile,code,time):
    sdk = SmsSDK(accId, accToken, appId)
    tid = '1'
    datas = (code, time)
    resp = sdk.sendMessage(tid, mobile, datas)
    result = json.loads(resp)
    if result['statusCode'] == '000000':
        return 1
    return 2

#测试
class Text(APIView):
    def get(self,request):
        num = random.randint(100000,999999)
        print(num)
        send_message('15109556983',num,1)
        return Response({'code':200})

#添加新闻
class MNewmanage(APIView):
    def post(self,request):
        ser = MNewSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code":200})
        else:
            print(ser.errors)
            return Response({"code": 20010})

    def get(self,request):
        cpage = request.GET.get('cpage')
        if cpage:
            new = MNew.objects.all()
            news = Paginator(new,2)
            new_cpage = news.page(cpage)
            ser = MNewSer(new_cpage,many=True)
            tpage = news.num_pages
            return Response({"code": 200, 'list': ser.data,'tpage':tpage})
        else:
            new = MNew.objects.all()
            ser = MNewSer(new,many=True)
            return Response({"code":200,'list':ser.data})


#添加广告
class Madvertisingmanage(APIView):
    def post(self, request):
        ser = MadvertisingmanageSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code": 200})
        else:
            print(ser.errors)
            return Response({"code": 20010})

    def get(self, request):
        cpage = request.GET.get('cpage')
        if cpage:
            advertising = Madvertising.objects.all()
            advertisings = Paginator(advertising, 2)
            advertisings_cpage = advertisings.page(cpage)
            ser = MadvertisingmanageSer(advertisings_cpage, many=True)
            tpage = advertisings.num_pages
            return Response({"code": 200, 'list': ser.data, 'tpage': tpage})
        else:
            advertising = Madvertising.objects.all()
            ser = MadvertisingmanageSer(advertising, many=True)
            return Response({"code": 200, 'list': ser.data})

#友情链接
class Friendlink(APIView):
    def post(self,request):
        link = request.GET.get('link')
        ser = MadvertisingmanageSer(data=request.data)
        if ser.is_valid():
            ser.save()
            # addlink = Madvertising.objects.all(id=link).frist()
            # addlink.count +=1
            # addlink.save()
            return Response({"code": 200})
        else:
            print(ser.errors)
            return Response({"code": 20010})


#美多 开始
import re
#注册页面
class Registermanage(APIView):
    def post(self,request):
#验证用户名
        #获取用户名
        username = request.data.get('username')
        #用户名是否为空
        if not username:
            return Response({'code':10010})
        #用户名是否符合规则 正则
        if not re.match('^\w{5,10}$',username):
            return Response({'code': 10011})
        #用户名是否唯一
        user = User.objects.filter(username=username).first()
        if user:
            return Response({'code': 10012})
#验证密码
        # 获取密码
        password = request.data.get('password')
        # 密码是否为空
        if not password:
            return Response({'code': 10016})
        # 密码是否符合规则 正则
        if not re.match('^[a-zA-Z0-9]{8,20}$', password):
            return Response({'code': 10017})
        # 密码是否注册
        pas = User.objects.filter(password=password).first()
        if pas:
            return Response({'code': 10018})

#确认密码
        conpassword = request.data.get('conpassword')
        print(conpassword)
        if conpassword == password:
            return Response({'code':10019})
        else:
            return Response({"code":10020})

#验证手机号
        # 获取手机号
        mobile = request.data.get('mobile')
        # 手机号是否为空
        if not mobile:
            return Response({'code': 10013})
        # 手机号是否符合规则 正则
        if not re.match('^1[3-9]\d{9}$', mobile):
            return Response({'code': 10014})
        # 手机号是否注册
        mob = User.objects.filter(mobile=mobile).first()
        if mob:
            return Response({'code': 10015})


        return Response({'code':200})














